package com.example.himalaya.views;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.adapters.PopWindowsPlayListRvAdapter;
import com.example.himalaya.base.BaseApplication;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import java.util.List;

/**
 * 播放列表弹出窗口
 */
public class PlayListPopupWindow extends PopupWindow {
    private static final String TAG = "PlayListPopupWindow";

    private final View mPopView;
    private TextView mPlayListCloseBtn;
    private RecyclerView mPlayListRv;
    private PopWindowsPlayListRvAdapter mPlayListRvAdapter;
    private TextView mPlayModeTv;
    private ImageView mPlayModeIv;
    private View mPlayModeContainer;
    private PlayListActionListener mPlayListActionListener =null;
    private View mPlayListOrderContainer;
    private ImageView mPlayListOrderIv;
    private TextView mPlayListOrderTv;

    public PlayListPopupWindow() {
        mPopView = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.pop_play_list, null);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        //设置setOutsideTouchable之前，要先设置setBackgroundDrawable，否则点击外部无法关闭pop
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setContentView(mPopView);
        //设置窗口进入和推出的动画
        setAnimationStyle(R.style.pop_animation);
        initView();
        initEvent();
    }

    private void initView() {
        mPlayListCloseBtn = mPopView.findViewById(R.id.play_list_close_btn);
        mPlayListRv = mPopView.findViewById(R.id.play_list_rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContentView().getContext());
        mPlayListRv.setLayoutManager(linearLayoutManager);
        mPlayListRvAdapter = new PopWindowsPlayListRvAdapter();
        mPlayListRv.setAdapter(mPlayListRvAdapter);
        mPlayModeTv = mPopView.findViewById(R.id.play_list_play_mode_switch_tv);
        mPlayModeIv = mPopView.findViewById(R.id.play_list_play_mode_switch_iv);
        mPlayModeContainer = mPopView.findViewById(R.id.play_list_play_mode_switch_container);
        mPlayListOrderContainer = mPopView.findViewById(R.id.play_list_order_container);
        mPlayListOrderIv = mPopView.findViewById(R.id.play_list_order_iv);
        mPlayListOrderTv = mPopView.findViewById(R.id.play_list_order_tv);
    }

    private void initEvent() {
        mPlayListCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //窗口消失
                PlayListPopupWindow.this.dismiss();
            }
        });

        mPlayModeContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换播放模式
                if (mPlayListActionListener != null) {
                    mPlayListActionListener.onPlayModeCLick();
                }
            }
        });
        mPlayListOrderContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //切换列表排序方式
                mPlayListActionListener.onOrderClick();
            }
        });
    }

    public void setPlayListData(List<Track> tracks) {
        if (mPlayListRvAdapter != null) {
            mPlayListRvAdapter.setData(tracks);
        }
    }


    public void setCurrentPlayPosition(int index) {
        mPlayListRvAdapter.setCurrentPlayPosition(index);
            //滚动到可视区域
            mPlayListRv.scrollToPosition(index);
    }

    public void setPlayListItemClickListener(PlayListItemClickListener listener){
        mPlayListRvAdapter.setOnItemClicker(listener);
    }

    public void updatePlayMode(XmPlayListControl.PlayMode currentMode) {
        int resId = R.drawable.selector_player_mode_loop;
        String[] playModes = getContentView().getContext().getResources().getStringArray(R.array.play_mode);
        String playMode = null;
        switch (currentMode) {
            case PLAY_MODEL_LIST_LOOP:
                resId=R.drawable.selector_player_mode_loop;
                playMode = playModes[0];
                break;
            case PLAY_MODEL_RANDOM:
                resId=R.drawable.selector_player_mode_loop_random;
                playMode = playModes[1];
                break;
            case PLAY_MODEL_SINGLE_LOOP:
                resId=R.drawable.selector_player_mode_loop_one;
                playMode = playModes[2];
                break;
        }
        mPlayModeIv.setImageResource(resId);
        mPlayModeTv.setText(playMode);
    }

    public void updatePlayListOrder(boolean isReverse) {
        if (!isReverse) {
            mPlayListOrderIv.setImageResource(R.drawable.selector_player_sort_asc);
            mPlayListOrderTv.setText("正序");
        } else {
            mPlayListOrderIv.setImageResource(R.drawable.selector_player_sort_desc);
            mPlayListOrderTv.setText("倒序");
        }
    }


    public interface PlayListItemClickListener {
        void onItemClick(int position);
    }


    public void setPlayListActionListener(PlayListActionListener listener) {
        mPlayListActionListener = listener;
    }

    public interface PlayListActionListener {
        //播放模式被点击
        void onPlayModeCLick();

        //列表排序被点击
        void onOrderClick();
    }
}
